﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using dlCentral.Utils;
using dlCentral.Scrapper;
using dlCentral.GUI;
using dlCentral.Settings.Data;

namespace dlCentral.PluginHandlers
{
       public class MediaDetail
        {

            public string Name { get; set; }
            public DateTime Date { get; set; } 
            public string DateStr { get; set; }
            public string RemoteDir { get; set; }
            public SiteSearchType SearchType { get; set; }
            public SettingsSite Site { get; set; }

            public string TorrentInfo { get; set; }
            public string TorrentURL { get; set; }

           
           public bool IsInfoChecked { get; set; }
            public int? MediaId { get; set; }

             
            public string MovieDBId { get; set; }

            public string DetailsURL { get; set; }

            public string Summary { get; set; }
           
            public int Runtime { get; set; }

            public string ImdbIDStr
            {
                get
                {
                    return "tt" + ImdbID;
                }
            }
            public string ImdbID
            {
                get
                {
                    return _imdbID;
                }
                set
                {
                    if (!string.IsNullOrEmpty(value))
                    {
                        _imdbID = value.Replace("tt", "").PadLeft(7, '0');
                        if (!dlCentralUtils.isImdbIdCorrect("tt" + _imdbID))
                        {
                            _imdbID = string.Empty;
                        }
                    }
                    else
                    {
                        _imdbID = string.Empty;
                    }
                }
            }
            private string _imdbID;

            public string Title { get; set; }

            public string YearStr
            {
                get
                {
                    if (dlCentralUtils.isYearCorrect(Year.ToString()))
                    {
                        return Year.ToString();
                    }
                    return string.Empty;
                }
            }
            public int Year { get; set; }

           public void FillInfo()
           {
                IsInfoChecked = true;
                TheMovieDB m2 = new TheMovieDB();
                string str = "";
                try
                {
                    str = System.IO.Path.GetFileName(Name);
                }
                catch
                {
                    str = Name;
                }
                int year;
                str = dlCentralUtils.extractYearFromTitle(str, out year);
                if (year == 0) str = dlCentralUtils.extractTitle(str);
                str = dlCentralUtils.RemoveGarbage(str);
                Title = str;
                MediaDetail m1 = new MediaDetail();
                m1.Title = Title;
                bool b;
                if (year != 0)
                {
                    b = m2.Search( ref m1, year);
                }
                else
                {
                    b = m2.Search(ref m1);
                }
                if (b)
                {
                    this.MovieDBId = m1.MovieDBId;
                    this.Title = m1.Title;
                    this.Year = m1.Year;
                    this.ImdbID = m1.ImdbID;
                    this.DetailsURL = m1.DetailsURL;
                    this.Plot = m1.Plot;
                    this.Runtime = m1.Runtime;
                    this.Thumb = m1.Thumb;

                }

       }

           
           public string SeasonStr
            {
                get
                {
                    if (dlCentralUtils.isSeasonOrEpisodeCorrect(Season.ToString()))
                    {
                        return string.Format("{0:00}", Season);
                    }
                    return string.Empty;
                }
            }
            public int Season { get; set; }

            public string EpisodeStr
            {
                get
                {
                    if (dlCentralUtils.isSeasonOrEpisodeCorrect(Episode.ToString()))
                    {
                        return string.Format("{0:00}", Episode);
                    }
                    return string.Empty;
                }
            }
            public int Episode { get; set; }

            public string Thumb { get; set; }

            public string FanArt { get; set; }

            public List<FileInfo> Files { get; set; }

           public string Plot { get; set; }

        }
    }

